<template>
  <div class="register-container">
    <!-- 顶部logo区域 -->
    <div class="header">
      <i class="el-icon-arrow-left back-icon" @click="goToHome"></i>
      <div class="logo-container" @click="goToHome">
        <span class="platform-title">实验室预约平台</span>
      </div>
    </div>
    <div class="register-box">
      <h2 class="register-title">用户注册</h2>
      <el-form
        ref="registerForm"
        :model="registerForm"
        :rules="registerRules"
        label-width="100px"
        class="register-form"
      >
        <el-form-item label="姓名" prop="u_name">
          <el-input
            v-model="registerForm.u_name"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>

        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model="registerForm.phone"
            placeholder="请输入联系电话"
          ></el-input>
        </el-form-item>

        <!-- 添加密码输入框 -->
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="registerForm.password"
            show-password
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>

        <!-- 添加确认密码输入框 -->
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            type="password"
            v-model="registerForm.confirmPassword"
            show-password
            placeholder="请确认密码"
          ></el-input>
        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="身份" prop="identity">
              <el-cascader
                v-model="registerForm.identity"
                :options="identityOptions"
                placeholder="请选择身份"
              ></el-cascader>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="地区" prop="region">
              <el-cascader
                v-model="registerForm.region"
                :options="regionOptions"
                placeholder="请选择地区"
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="企业" prop="company">
              <el-select
                v-model="registerForm.company"
                placeholder="请选择企业"
              >
                <el-option
                  v-for="item in companyOptions"
                  :key="item.eid"
                  :label="item.ename"
                  :value="item.eid"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行业类型" prop="industry">
              <el-radio-group
                v-model="registerForm.industry"
                class="radio-group"
              >
                <el-radio label="0">新能源</el-radio>
                <el-radio label="1">非新能源</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item class="button-group">
          <el-button type="primary" @click="handleRegister">注册</el-button>
          <el-button  type="warning" @click="reset">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { entList, adrsList, register, statusList } from "@/api/syspt/syspt";
export default {
  name: "sysregister",
  data() {
    return {
      registerForm: {
        u_name: "",
        phone: "",
        identity: [],
        region: "",
        company: "",
        industry: "",
        password: "",
        confirmPassword: "",
      },
      registerRules: {
        u_name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        phone: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
        identity: [
          { required: true, message: "请选择身份", trigger: "change" },
        ],
        region: [{ required: true, message: "请选择地区", trigger: "change" }],
        company: [{ required: true, message: "请选择企业", trigger: "change" }],
        industry: [
          { required: true, message: "请选择行业类型", trigger: "change" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            message: "密码长度不能少于6位",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          { required: true, message: "请确认密码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value !== this.registerForm.password) {
                callback(new Error("两次输入的密码不一致"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
      // 身份选项
      identityOptions: [],
      // 地区选项
      regionOptions: [],
      // 企业选项
      companyOptions: [],
    };
  },
  methods: {
    //
    
    reset() {
      this.registerForm= {
        u_name: "",
        phone: "",
        identity: [],
        region: "",
        company: "",
        industry: "",
        password: "",
        confirmPassword: "",
      }
    },
    // 注册
    handleRegister() {
      register(this.registerForm).then((response) => {
        if (response.code === 200) {
          this.$message.success("注册成功");
          this.$router.push("/syshome");
        } else {
          this.$message.error("注册失败");
        }
      });
    },
    // 公司
    fetchCompanyOptions() {
      entList().then((response) => {
        this.companyOptions = response.rows;
      });
    },
    // 地区
    fetchRegionOptions() {
      adrsList().then((response) => {
        this.regionOptions = response.data.map((city) => ({
          value: city.id,
          label: city.name,
          children: city.adrlist.map((region) => ({
            value: region.id,
            label: region.name,
          })),
        }));
      });
    },

    //身份
    fetchIdentityOptions() {
      statusList().then((response) => {
          this.identityOptions = response.data.map((status) => ({
            value: status.sid,
            label: status.sname,
            children: status.persons.map((identity) => ({
              value: identity.pid,
              label: identity.pname,
            })),
          }));
      });
    },

    handleCancel() {
      this.$router.go(-1);
    },

    // 返回首页
    goToHome() {
      this.$router.push("/syshome");
    },

    handleCancel() {
      this.$router.go(-1);
    },
    togglePasswordVisibility() {
      this.$refs.registerForm.fields.find(
        (field) => field.prop === "password"
      ).type =
        this.$refs.registerForm.fields.find(
          (field) => field.prop === "password"
        ).type === "password"
          ? "text"
          : "password";
    },
    toggleConfirmPasswordVisibility() {
      this.$refs.registerForm.fields.find(
        (field) => field.prop === "confirmPassword"
      ).type =
        this.$refs.registerForm.fields.find(
          (field) => field.prop === "confirmPassword"
        ).type === "password"
          ? "text"
          : "password";
    },
  },
  mounted() {
    // 初始化时获取企业信息
    this.fetchCompanyOptions();
    // 初始化时获取地区信息
    this.fetchRegionOptions();
    // 初始化时获取身份信息
    this.fetchIdentityOptions();
  
  },
};
</script>



<style scoped>
.logo-container {
  cursor: pointer;
}

.header {
  position: absolute;
  top: 0;
  left: 0;
  height: 80px;
  padding-left: 12.5vw;
  display: flex;
  align-items: center;
  gap: 15px;
}

.back-icon {
  font-size: 24px;
  color: #409eff;
  cursor: pointer;
}

.back-icon:hover {
  color: #66b1ff; /* 悬停时稍微亮一点的蓝色 */
}

.logo-container {
  cursor: pointer;
}

.platform-title {
  font-size: 24px;
  font-weight: bold;
  color: #409eff;
}

.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.register-box {
  width: 40%; /* 将宽度从50%调整为40% */
  padding: 40px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.register-title {
  text-align: center;
  margin-bottom: 40px;
  color: #303133;
  font-size: 28px;
  font-weight: 500;
}

.register-form {
  max-width: 100%; /* 设置最大宽度为100% */
}

.el-input,
.el-select,
.el-cascader {
  width: 100%;
}

.button-group {
  text-align: center;
  margin-top: 40px;
}

.el-button {
  padding: 12px 35px;
  margin: 0 10px;
}

.el-radio-group {
  display: flex;
  gap: 20px;
}

.radio-group {
  display: flex;
  justify-content: space-between;
  width: 100%;
  flex-wrap: wrap; /* 添加此行，使单选项在溢出时换行 */
}

.el-form-item {
  margin-bottom: 25px;
}

.el-form-item__label {
  font-weight: 500;
  color: #606266;
  text-align: left; /* 设置标签文本左对齐 */
  width: 100%; /* 设置标签文本宽度为100% */
}

.el-form-item__label::after {
  content: "";
  display: inline-block;
  width: 100%;
  height: 0;
}

.el-input__inner {
  height: 40px;
  line-height: 40px;
}

.el-select .el-input__inner {
  height: 40px;
}

.el-cascader .el-input__inner {
  height: 40px;
}

.password-icon {
  cursor: pointer;
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
}
</style>
